Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(table): Respect falsy value for addDuplicateNumbers #672

Merged
merged 3 commits into from
Jan 16, 2025

Conversation

tom-osborne
Copy link
Member

The logic for the third parameter (addDuplicateNumbers) does not respect a falsy value.

addDuplicateNumbers = addDuplicateNumbers ~= nil and addDuplicateNumbers or true

When a value of false is passed the logic is resolved as follows:

  1. addDuplicateNumbers ~= nil resolves to true
  2. addDuplicateNumbers ~= nil and addDuplicateNumbers is therefore the same as true and false which resolves to false
  3. addDuplicateNumbers ~= nil and addDuplicateNumbers or true is therefore the same a false or true, which resolves to true

So a falsy value always coerces to true.

Simple test script:

local t1 = { 1 }
local t2 = { 1 }

local tmerged = lib.table.merge(t1, t2, false)
print("Merged", json.encode(tmerged))
--------
-- Output:
-- Merged   [2]

Proposed solution is to reverse the logic somewhat so it sets the value to true only when nil.

addDuplicateNumbers = addDuplicateNumbers == nil and true or addDuplicateNumbers

Running the same test script:

-- Output:
-- Merged   [1]

@tom-osborne
Copy link
Member Author

See also #671

Copy link
Member

@jag3dagster jag3dagster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic checks out @thelindat

@jag3dagster
Copy link
Member

Thank you for the detailed explanation @tom-osborne

@@ -85,7 +85,7 @@ end
---@return table
---Merges two tables together. Defaults to adding duplicate keys together if they are numbers, otherwise they are overriden.
local function table_merge(t1, t2, addDuplicateNumbers)
addDuplicateNumbers = addDuplicateNumbers ~= nil and addDuplicateNumbers or true
addDuplicateNumbers = addDuplicateNumbers == nil and true or addDuplicateNumbers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addDuplicateNumbers = addDuplicateNumbers == nil or addDuplicateNumbers

Copy link
Member Author

@tom-osborne tom-osborne Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thelindat Changes made as requested. Thanks

@thelindat thelindat merged commit d142d8d into overextended:master Jan 16, 2025
@tom-osborne tom-osborne requested a review from thelindat January 16, 2025 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants